/*
 * Copyright (c) 1983, 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Rick Macklem at The University of Guelph.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1983, 1989, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "from: @(#)nfsstat.c	8.1 (Berkeley) 6/6/93";
static char *rcsid = "$NetBSD: nfsstat.c,v 1.7 1996/03/03 17:21:30 thorpej Exp $";
#else
static char *rcsid = "$OpenBSD: nfsstat.c,v 1.7 1998/07/05 18:42:43 deraadt Exp $";
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <sys/vnode.h>
#include <netinet/in.h>
#include <newnfs/nfs/rpcv2.h>
#include <newnfs/nfs/nfsproto.h>
#include <newnfs/nfs/nfskpiport.h>
#include <newnfs/nfs/nfs.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <paths.h>
#include <err.h>

#define	DUMPSIZE	10000

#define SHOW_SERVER 0x01
#define SHOW_CLIENT 0x02
#define SHOW_ALL (SHOW_SERVER | SHOW_CLIENT)

int printtitle = 1;
struct nfsstats nfsstats;

void intpr(u_int), printhdr(void), sidewaysintpr(u_int, u_int), usage(void);
void dump_lockstate(char *), dump_openstate(void);
char *open_flags(u_int32_t), *deleg_flags(u_int32_t), *lock_flags(u_int32_t);
char *client_flags(u_int32_t);

main(int argc, char **argv)
{
	extern int optind;
	extern char *optarg;
	u_int interval;
	u_int display = SHOW_ALL;
	int ch, openstate;
	char *lockfile;

	openstate = interval = 0;
	lockfile = NULL;
	while ((ch = getopt(argc, argv, "w:scol:q")) != -1)
		switch (ch) {
		case 'w':
			interval = atoi(optarg);
			break;
		case 's':
			display = SHOW_SERVER;
			break;
		case 'c':
			display = SHOW_CLIENT;
			break;
		case 'o':
			openstate = 1;
			break;
		case 'l':
			lockfile = optarg;
			break;
		case 'q':
			printtitle = 0;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

#define	BACKWARD_COMPATIBILITY
#ifdef	BACKWARD_COMPATIBILITY
	if (*argv) {
		interval = atoi(*argv);
	}
#endif
	if (openstate && (interval || lockfile ||
	    display != SHOW_ALL)) {
	    fprintf(stderr, "-o cannot be used with -w, -s, -c, -l\n");
	    exit(1);
	}
	if (lockfile && (interval || openstate ||
	    display != SHOW_ALL)) {
	    fprintf(stderr, "-l cannot be used with -w, -s, -c, -o\n");
	    exit(1);
	}

	/*
	 * For -o, dump all open/lock state.
	 */
	if (openstate) {
		dump_openstate();
		exit(0);
	}

	/*
	 * For -l, dump lock state for that file.
	 */
	if (lockfile) {
		dump_lockstate(lockfile);
		exit(0);
	}

	if (nfssvc(NFSSVC_GETSTATS, &nfsstats) < 0) {
		fprintf(stderr, "Can't get stats, %s\n", strerror(errno));
		exit(1);
	}

	if (interval)
		sidewaysintpr(interval, display);
	else
		intpr(display);
	exit(0);
}

/*
 * Print a description of the nfs stats.
 */
void
intpr(u_int display)
{

	if (display & SHOW_CLIENT) {
		if (printtitle) {
		    printf("Client Info:\n");
		    printf("Rpc Counts:\n");
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Getattr", "Setattr", "Lookup", "Readlink", "Read",
		       "Write", "Create", "Remove");
		}
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.rpccnt[NFSPROC_GETATTR],
		       nfsstats.rpccnt[NFSPROC_SETATTR],
		       nfsstats.rpccnt[NFSPROC_LOOKUP],
		       nfsstats.rpccnt[NFSPROC_READLINK],
		       nfsstats.rpccnt[NFSPROC_READ],
		       nfsstats.rpccnt[NFSPROC_WRITE],
		       nfsstats.rpccnt[NFSPROC_CREATE],
		       nfsstats.rpccnt[NFSPROC_REMOVE]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
		       "Readdir", "RdirPlus", "Access");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.rpccnt[NFSPROC_RENAME],
		       nfsstats.rpccnt[NFSPROC_LINK],
		       nfsstats.rpccnt[NFSPROC_SYMLINK],
		       nfsstats.rpccnt[NFSPROC_MKDIR],
		       nfsstats.rpccnt[NFSPROC_RMDIR],
		       nfsstats.rpccnt[NFSPROC_READDIR],
		       nfsstats.rpccnt[NFSPROC_READDIRPLUS],
		       nfsstats.rpccnt[NFSPROC_ACCESS]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
			"SetClId", "SetClIdCf", "Lock");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.rpccnt[NFSPROC_MKNOD],
		       nfsstats.rpccnt[NFSPROC_FSSTAT],
		       nfsstats.rpccnt[NFSPROC_FSINFO],
		       nfsstats.rpccnt[NFSPROC_PATHCONF],
		       nfsstats.rpccnt[NFSPROC_COMMIT],
		       nfsstats.rpccnt[NFSPROC_SETCLIENTID],
		       nfsstats.rpccnt[NFSPROC_SETCLIENTIDCFRM],
		       nfsstats.rpccnt[NFSPROC_LOCK]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s\n",
		       "LockT", "LockU", "Open", "OpenCfr");
		printf("%9d %9d %9d %9d\n",
		       nfsstats.rpccnt[NFSPROC_LOCKT],
		       nfsstats.rpccnt[NFSPROC_LOCKU],
		       nfsstats.rpccnt[NFSPROC_OPEN],
		       nfsstats.rpccnt[NFSPROC_OPENCONFIRM]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
			"OpenOwner", "Opens", "LockOwner",
			"Locks", "Delegs", "LocalOwn",
			"LocalOpen", "LocalLOwn");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
			nfsstats.clopenowners,
			nfsstats.clopens,
			nfsstats.cllockowners,
			nfsstats.cllocks,
			nfsstats.cldelegates,
			nfsstats.cllocalopenowners,
			nfsstats.cllocalopens,
			nfsstats.cllocallockowners);
		if (printtitle)
		    printf("%9.9s\n", "LocalLock");
		printf("%9d\n", nfsstats.cllocallocks);
		if (printtitle) {
		    printf("Rpc Info:\n");
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
		}
		printf("%9d %9d %9d %9d %9d\n",
		       nfsstats.rpctimeouts,
		       nfsstats.rpcinvalid,
		       nfsstats.rpcunexpected,
		       nfsstats.rpcretries,
		       nfsstats.rpcrequests);
		if (printtitle) {
		    printf("Cache Info:\n");
		    printf("%9.9s %9.9s %9.9s %9.9s",
		       "Attr Hits", "Misses", "Lkup Hits", "Misses");
		    printf(" %9.9s %9.9s %9.9s %9.9s\n",
		       "BioR Hits", "Misses", "BioW Hits", "Misses");
		}
		printf("%9d %9d %9d %9d",
		       nfsstats.attrcache_hits, nfsstats.attrcache_misses,
		       nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
		printf(" %9d %9d %9d %9d\n",
		       nfsstats.biocache_reads,
		       nfsstats.read_bios,
		       nfsstats.biocache_writes,
		       nfsstats.write_bios);
		if (printtitle) {
		    printf("%9.9s %9.9s %9.9s %9.9s",
		       "BioRLHits", "Misses", "BioD Hits", "Misses");
		    printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
		}
		printf("%9d %9d %9d %9d",
		       nfsstats.biocache_readlinks,
		       nfsstats.readlink_bios,
		       nfsstats.biocache_readdirs,
		       nfsstats.readdir_bios);
		printf(" %9d %9d\n",
		       nfsstats.direofcache_hits, nfsstats.direofcache_misses);
	}
	if (display & SHOW_SERVER) {
		if (printtitle) {
		    printf("\nServer Info:\n");
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Getattr", "Setattr", "Lookup", "Readlink", "Read",
		       "Write", "Create", "Remove");
		}
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvrpccnt[NFSV4OP_GETATTR],
		       nfsstats.srvrpccnt[NFSV4OP_SETATTR],
		       nfsstats.srvrpccnt[NFSV4OP_LOOKUP],
		       nfsstats.srvrpccnt[NFSV4OP_READLINK],
		       nfsstats.srvrpccnt[NFSV4OP_READ],
		       nfsstats.srvrpccnt[NFSV4OP_WRITE],
		       nfsstats.srvrpccnt[NFSV4OP_V3CREATE],
		       nfsstats.srvrpccnt[NFSV4OP_REMOVE]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
		       "Readdir", "RdirPlus", "Access");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvrpccnt[NFSV4OP_RENAME],
		       nfsstats.srvrpccnt[NFSV4OP_LINK],
		       nfsstats.srvrpccnt[NFSV4OP_SYMLINK],
		       nfsstats.srvrpccnt[NFSV4OP_MKDIR],
		       nfsstats.srvrpccnt[NFSV4OP_RMDIR],
		       nfsstats.srvrpccnt[NFSV4OP_READDIR],
		       nfsstats.srvrpccnt[NFSV4OP_READDIRPLUS],
		       nfsstats.srvrpccnt[NFSV4OP_ACCESS]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Mknod", "Fsstat", "Fsinfo", "PathConf",
		       "Commit", "LookupP", "SetClId", "SetClIdCf");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvrpccnt[NFSV4OP_MKNOD],
		       nfsstats.srvrpccnt[NFSV4OP_FSSTAT],
		       nfsstats.srvrpccnt[NFSV4OP_FSINFO],
		       nfsstats.srvrpccnt[NFSV4OP_PATHCONF],
		       nfsstats.srvrpccnt[NFSV4OP_COMMIT],
		       nfsstats.srvrpccnt[NFSV4OP_LOOKUPP],
		       nfsstats.srvrpccnt[NFSV4OP_SETCLIENTID],
		       nfsstats.srvrpccnt[NFSV4OP_SETCLIENTIDCFRM]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Open", "OpenAttr", "OpenDwnGr", "OpenCfrm",
		       "DelePurge", "DeleRet", "GetFH", "Lock");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvrpccnt[NFSV4OP_OPEN],
		       nfsstats.srvrpccnt[NFSV4OP_OPENATTR],
		       nfsstats.srvrpccnt[NFSV4OP_OPENDOWNGRADE],
		       nfsstats.srvrpccnt[NFSV4OP_OPENCONFIRM],
		       nfsstats.srvrpccnt[NFSV4OP_DELEGPURGE],
		       nfsstats.srvrpccnt[NFSV4OP_DELEGRETURN],
		       nfsstats.srvrpccnt[NFSV4OP_GETFH],
		       nfsstats.srvrpccnt[NFSV4OP_LOCK]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "LockT", "LockU", "Close", "Verify", "NVerify",
		       "PutFH", "PutPubFH", "PutRootFH");
		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvrpccnt[NFSV4OP_LOCKT],
		       nfsstats.srvrpccnt[NFSV4OP_LOCKU],
		       nfsstats.srvrpccnt[NFSV4OP_CLOSE],
		       nfsstats.srvrpccnt[NFSV4OP_VERIFY],
		       nfsstats.srvrpccnt[NFSV4OP_NVERIFY],
		       nfsstats.srvrpccnt[NFSV4OP_PUTFH],
		       nfsstats.srvrpccnt[NFSV4OP_PUTPUBFH],
		       nfsstats.srvrpccnt[NFSV4OP_PUTROOTFH]);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Renew", "RestoreFH", "SaveFH", "Secinfo",
		       "RelLckOwn", "V4Create");
		printf("%9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvrpccnt[NFSV4OP_RENEW],
		       nfsstats.srvrpccnt[NFSV4OP_RESTOREFH],
		       nfsstats.srvrpccnt[NFSV4OP_SAVEFH],
		       nfsstats.srvrpccnt[NFSV4OP_SECINFO],
		       nfsstats.srvrpccnt[NFSV4OP_RELEASELCKOWN],
		       nfsstats.srvrpccnt[NFSV4OP_CREATE]);
		if (printtitle) {
		    printf("Server:\n");
		    printf("%9.9s %9.9s %9.9s\n",
			"Retfailed", "Faults", "Clients");
		}
		printf("%9d %9d %9d\n",
			nfsstats.srv_errs, nfsstats.srvrpc_errs,
			nfsstats.srvclients);
		if (printtitle)
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s \n",
			"OpenOwner", "Opens", "LockOwner",
			"Locks", "Delegs");
		printf("%9d %9d %9d %9d %9d \n",
			nfsstats.srvopenowners,
			nfsstats.srvopens,
			nfsstats.srvlockowners,
			nfsstats.srvlocks,
			nfsstats.srvdelegates);
		if (printtitle) {
		    printf("Server Cache Stats:\n");
		    printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
		       "Inprog", "Idem", "Non-idem", "Misses", "CacheSize",
		       "TCPPeak", "GSSCsize");
		}
		printf("%9d %9d %9d %9d %9d %9d %9d\n",
		       nfsstats.srvcache_inproghits,
		       nfsstats.srvcache_idemdonehits,
		       nfsstats.srvcache_nonidemdonehits,
		       nfsstats.srvcache_misses,
		       nfsstats.srvcache_size,
		       nfsstats.srvcache_tcppeak,
		       nfsstats.gsscache_size);
	}
}

u_char	signalled;			/* set if alarm goes off "early" */

/*
 * Print a running summary of nfs statistics.
 * Repeat display every interval seconds, showing statistics
 * collected over that interval.  Assumes that interval is non-zero.
 * First line printed at top of screen is always cumulative.
 */
void
sidewaysintpr(u_int interval, u_int display)
{
	struct nfsstats lastst;
	int hdrcnt, oldmask;
	void catchalarm();

	(void)signal(SIGALRM, catchalarm);
	signalled = 0;
	(void)alarm(interval);
	bzero((caddr_t)&lastst, sizeof(lastst));

	for (hdrcnt = 1;;) {
		if (!--hdrcnt) {
			printhdr();
			hdrcnt = 20;
		}
		if (display & SHOW_CLIENT)
		  printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
		    nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
		    nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
		    nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
		    nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
		    nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
		    nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
		    nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
		    (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
		    +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
		if (display & SHOW_SERVER)
		  printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
		    nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
		    nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
		    nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
		    nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
		    nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
		    nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
		    nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
		    (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
		    +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
		lastst = nfsstats;
		fflush(stdout);
		oldmask = sigblock(sigmask(SIGALRM));
		if (!signalled)
			sigpause(0);
		sigsetmask(oldmask);
		signalled = 0;
		(void)alarm(interval);
	}
	/*NOTREACHED*/
}

void
printhdr(void)
{
	if (printtitle)
	  printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
	    "Access", "Readdir");
	fflush(stdout);
}

/*
 * Called if an interval expires before sidewaysintpr has completed a loop.
 * Sets a flag to not wait for the alarm.
 */
void
catchalarm(void)
{
	signalled = 1;
}

void
usage(void)
{
	fprintf(stderr,
	    "usage: nfsstat [-M core] [-N system] [-w interval]");
	fprintf(stderr,
	    " [-s] [-c] [-o] [-l file] [-q]\n");
	exit(1);
}

struct nfsd_dumpclients dp[DUMPSIZE];
/*
 * Dump all open/lock state.
 */
void
dump_openstate(void)
{
	struct nfsd_dumplist dumplist;
	int cnt, i;

	dumplist.ndl_size = DUMPSIZE;
	dumplist.ndl_list = (void *)dp;
	if (nfssvc(NFSSVC_DUMPCLIENTS, &dumplist) < 0) {
		fprintf(stderr, "Can't perform dump clients syscall\n");
		exit(1);
	}

	if (printtitle)
	    printf("%-13s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %-15s %s\n",
		"Flags", "OpenOwner", "Open", "LockOwner",
		"Lock", "Deleg", "OldDeleg", "Clientaddr", "ClientID");
	/*
	 * Loop through results, printing them out.
	 */
	cnt = 0;
	while (dp[cnt].ndcl_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
		printf("%-13s ", client_flags(dp[cnt].ndcl_flags));
		printf("%9d %9d %9d %9d %9d %9d ",
			dp[cnt].ndcl_nopenowners,
			dp[cnt].ndcl_nopens,
			dp[cnt].ndcl_nlockowners,
			dp[cnt].ndcl_nlocks,
			dp[cnt].ndcl_ndelegs,
			dp[cnt].ndcl_nolddelegs);
		if (dp[cnt].ndcl_addrfam == AF_INET)
			printf("%-15s ",
				inet_ntoa(dp[cnt].ndcl_cbaddr.sin_addr));
		for (i = 0; i < dp[cnt].ndcl_clid.nclid_idlen; i++)
			printf("%02x", dp[cnt].ndcl_clid.nclid_id[i]);
		printf("\n");
		cnt++;
	}
}

struct nfsd_dumplocks lp[DUMPSIZE];
/*
 * Dump the lock state for a file.
 */
void
dump_lockstate(char *fname)
{
	struct nfsd_dumplocklist dumplocklist;
	int cnt, i;

	dumplocklist.ndllck_size = DUMPSIZE;
	dumplocklist.ndllck_list = (void *)lp;
	dumplocklist.ndllck_fname = fname;
	if (nfssvc(NFSSVC_DUMPLOCKS, &dumplocklist) < 0) {
		fprintf(stderr, "Can't dump locks for %s\n", fname);
		exit(1);
	}

	if (printtitle)
		printf("%-11s %-36s %-15s %s\n",
			"Open/Lock",
			"          Stateid or Lock Range",
			"Clientaddr",
			"Owner and ClientID");
	/*
	 * Loop through results, printing them out.
	 */
	cnt = 0;
	while (lp[cnt].ndlck_clid.nclid_idlen > 0 && cnt < DUMPSIZE) {
		if (lp[cnt].ndlck_flags & NFSLCK_OPEN)
			printf("%-11s %9d %08x %08x %08x ",
				open_flags(lp[cnt].ndlck_flags),
				lp[cnt].ndlck_stateid.seqid,
				lp[cnt].ndlck_stateid.other[0],
				lp[cnt].ndlck_stateid.other[1],
				lp[cnt].ndlck_stateid.other[2]);
		else if (lp[cnt].ndlck_flags & (NFSLCK_DELEGREAD |
			 NFSLCK_DELEGWRITE))
			printf("%-11s %9d %08x %08x %08x ",
				deleg_flags(lp[cnt].ndlck_flags),
				lp[cnt].ndlck_stateid.seqid,
				lp[cnt].ndlck_stateid.other[0],
				lp[cnt].ndlck_stateid.other[1],
				lp[cnt].ndlck_stateid.other[2]);
		else
			printf("%-11s  %17lld %17lld ",
				lock_flags(lp[cnt].ndlck_flags),
				lp[cnt].ndlck_first,
				lp[cnt].ndlck_end);
		if (lp[cnt].ndlck_addrfam == AF_INET)
			printf("%-15s ",
				inet_ntoa(lp[cnt].ndlck_cbaddr.sin_addr));
		else
			printf("%-15s ",
				"  ");
		for (i = 0; i < lp[cnt].ndlck_owner.nclid_idlen; i++)
			printf("%02x", lp[cnt].ndlck_owner.nclid_id[i]);
		printf(" ");
		for (i = 0; i < lp[cnt].ndlck_clid.nclid_idlen; i++)
			printf("%02x", lp[cnt].ndlck_clid.nclid_id[i]);
		printf("\n");
		cnt++;
	}
}

/*
 * Parse the Open/Lock flag bits and create a string to be printed.
 */
char flag_string[20];

char *
open_flags(u_int32_t flags)
{
	int i, j;

	strlcpy(flag_string, "Open ", sizeof (flag_string));
	i = 5;
	if (flags & NFSLCK_READACCESS)
		flag_string[i++] = 'R';
	if (flags & NFSLCK_WRITEACCESS)
		flag_string[i++] = 'W';
	flag_string[i++] = ' ';
	flag_string[i++] = 'D';
	flag_string[i] = 'N';
	j = i;
	if (flags & NFSLCK_READDENY)
		flag_string[i++] = 'R';
	if (flags & NFSLCK_WRITEDENY)
		flag_string[i++] = 'W';
	if (i == j)
		i++;
	flag_string[i] = '\0';
	return (flag_string);
}

char *
deleg_flags(u_int32_t flags)
{

	if (flags & NFSLCK_DELEGREAD)
		strlcpy(flag_string, "Deleg R", sizeof (flag_string));
	else
		strlcpy(flag_string, "Deleg W", sizeof (flag_string));
	return (flag_string);
}

char *
lock_flags(u_int32_t flags)
{

	if (flags & NFSLCK_READ)
		strlcpy(flag_string, "Lock R", sizeof (flag_string));
	else
		strlcpy(flag_string, "Lock W", sizeof (flag_string));
	return (flag_string);
}

char *
client_flags(u_int32_t flags)
{

	flag_string[0] = '\0';
	if (flags & LCL_NEEDSCONFIRM)
		strlcat(flag_string, "NC ", sizeof (flag_string));
	if (flags & LCL_CALLBACKSON)
		strlcat(flag_string, "CB ", sizeof (flag_string));
	if (flags & LCL_GSS)
		strlcat(flag_string, "GSS ", sizeof (flag_string));
	if (flags & LCL_ADMINREVOKED)
		strlcat(flag_string, "REV", sizeof (flag_string));
	return (flag_string);
}
